package com.controller;

import com.entity.*;
import com.service.*;
import com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FangshiController
 * @Description 支付方式模块控制层
 */
@Controller
public class FangshiController extends BaseController{

    @Autowired
    private FangshiService fangshiService;




    //前台-支付方式列表页
    @RequestMapping("/fangshiManaQian")
    public ModelAndView fangshiManaQian(Fangshi req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("fangshiManaQian");
        mav.addObject("actionName","fangshiManaQian");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-支付方式图片列表页
    @RequestMapping("/fangshiManaQiantp")
    public ModelAndView fangshiManaQiantp(Fangshi req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("fangshiManaQiantp");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-支付方式管理列表页
    @RequestMapping("/fangshiMana")
    public ModelAndView fangshiMana(Fangshi req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("fangshiMana");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-支付方式查看列表页
    @RequestMapping("/fangshiChakan")
    public ModelAndView fangshiChakan(Fangshi req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("fangshiChakan");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-支付方式按id列排序列表页
    @RequestMapping("/fangshiManaQianSort")
    public ModelAndView fangshiManaQianSort(Fangshi  req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo) {
        ModelAndView mav = new ModelAndView("fangshiManaQian");
        mav.addObject("actionName","fangshiManaQianSort");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-支付方式详情页
    @GetMapping("/fangshiDetailQian/{id}")
    @ResponseBody
    public ModelAndView fangshiDetailQian(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("fangshiDetailQian");
        Fangshi info= fangshiService.findById(id);
        mav.addObject("fangshi", info);

        return mav;
    }

    //后台-支付方式详情页
    @GetMapping("/fangshiDetail/{id}")
    @ResponseBody
    public ModelAndView fangshiDetail(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("fangshiDetail");
        Fangshi info= fangshiService.findById(id);
        mav.addObject("fangshi", info);
        return mav;
    }

    //获取所有支付方式数据接口
    @RequestMapping("/fangshiAll")
    @ResponseBody
    public ApiResponse<Fangshi> fangshiAll(){
        List<Fangshi> fangshiList=fangshiService.findAll();
        return ApiResponse.success(fangshiList);
    }

    //根据条件（字符类型模糊匹配查询）获取支付方式数据接口
    @RequestMapping("/fangshiSearch")
    @ResponseBody
    public ApiResponse<Fangshi> fangshiSearch(Fangshi req){
        List<Fangshi> fangshiList=fangshiService.find(req);
        return ApiResponse.success(fangshiList);
    }

    //根据条件（字符类型完全匹配查询）获取支付方式数据接口
    @RequestMapping("/fangshiSearchByEqualTo")
    @ResponseBody
    public ApiResponse<Fangshi> fangshiSearchByEqualTo(Fangshi req){
        List<Fangshi> fangshiList=fangshiService.findByEqualTo(req);
        return ApiResponse.success(fangshiList);
    }

    //支付方式添加或修改接口
    @PostMapping("/fangshiAddOrUpdate")
    @ResponseBody
    public ApiResponse<Integer> fangshiAddOrUpdate(@RequestBody Fangshi req, HttpSession session, Boolean isUPdateSession){



        int result = fangshiService.saveOrUpdate(req);


        return ApiResponse.success(result);
    }

    //支付方式修改单独一个字段接口
    @PostMapping("/fangshiSet")
    @ResponseBody
    public ApiResponse<Integer> fangshiSet(Fangshi req){
        int result = fangshiService.saveOrUpdate(req);
        return ApiResponse.success(result);
    }



    //支付方式删除接口
    @RequestMapping("/fangshiDel/{id}")
    @ResponseBody
    public ApiResponse<Integer> fangshiDel(@PathVariable Integer id){
        int result = fangshiService.deleteById(id);
        return ApiResponse.success(result);
    }

    //后台-支付方式修改页
    @RequestMapping("/fangshiGet/{id}")
    @ResponseBody
    public ModelAndView fangshiGet(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("fangshiSet");
        Fangshi info = fangshiService.findById(id);
        mav.addObject("fangshi", info);
        return mav;
    }








    //支付方式模糊匹配查询分页数据封装
    private ModelAndView getModelAndView(Fangshi req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Fangshi> pageWrap=new PageWrap<Fangshi>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Fangshi> pageInfo= fangshiService.findPage(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);

        return mav;
    }

    //支付方式完全匹配查询分页数据封装
    private ModelAndView getModelAndViewByEqualTo(Fangshi req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Fangshi> pageWrap=new PageWrap<Fangshi>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Fangshi> pageInfo= fangshiService.findPageByEqualTo(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);

        return mav;
        }
}
